package com.example.stumanager;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.example.stumanager.admin.AdminHomePage;
import com.example.stumanager.database.UserSql;
import com.example.stumanager.student.StudentHomePage;
import com.example.stumanager.student.StudentRegisterPage;

/**
 * 首页登陆界面
 */
public class HomepageLoginInterface extends AppCompatActivity {
    public static String stuId = "";
    Button sign, login;
    EditText passWord, number;
    RadioGroup load_radiogroup;
    private CheckBox rememberPass;
    private boolean isRemember;
    private SharedPreferences pref;
    private SharedPreferences.Editor editor;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_page_login_interface);
        //注册
        sign = findViewById(R.id.sign);
        //登录
        login = findViewById(R.id.login);
        //账号
        number = findViewById(R.id.number);
        //密码
        passWord = findViewById(R.id.passWord);

        load_radiogroup = findViewById(R.id.rg_1);
        rememberPass = findViewById(R.id.checkbox_remember);
        pref = PreferenceManager.getDefaultSharedPreferences(this);
        isRemember = pref.getBoolean("remember_password", false);

        sign.setOnClickListener(v -> {
            Intent intent = new Intent();
            intent.setClass(HomepageLoginInterface.this, StudentRegisterPage.class);
            startActivity(intent);
        });

        login.setOnClickListener(v -> {
            UserSql u = new UserSql(getBaseContext());
            int checkID = load_radiogroup.getCheckedRadioButtonId();
            stuId = number.getText().toString();
            if (checkID == R.id.rb_1) {
                if (u.check(number.getText().toString(), passWord.getText().toString())) {
                    Intent intent = new Intent();
                    intent.setClass(HomepageLoginInterface.this, StudentHomePage.class);
                    startActivity(intent);
                    Toast.makeText(HomepageLoginInterface.this, "登录成功！", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(HomepageLoginInterface.this, "登录失败，姓名或学号错误！", Toast.LENGTH_SHORT).show();
                }
            } else if (checkID == R.id.rb_2) {
                //判断用户是否是管理员
                if (u.adminCheck(number.getText().toString(), passWord.getText().toString())) {
                    Toast.makeText(HomepageLoginInterface.this, "登录成功！", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent();
                    intent.setClass(HomepageLoginInterface.this, AdminHomePage.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(HomepageLoginInterface.this, "登录失败，密码或账号错误！", Toast.LENGTH_SHORT).show();
                }
            } else {
                Toast.makeText(HomepageLoginInterface.this, "您还没有选择任何登录类型！", Toast.LENGTH_SHORT).show();
            }


        });

        load_radiogroup.setOnCheckedChangeListener(
                (group, checkedId) -> {
                    switch (group.getCheckedRadioButtonId()) {
                        case R.id.rb_1:
                            String state = "student";
                            break;
                        case R.id.rb_2:
                            state = "manager";
                            break;
                    }

                });

        rememberPass.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (rememberPass.isChecked()) {
                System.out.println("记住密码已选中");
                pref.edit().putBoolean("ISCHECK", true).commit();
            } else {
                System.out.println("记住密码没有选中");
                pref.edit().putBoolean("ISCHECK", false).commit();

            }

        });

        //如果手机有记录
        if (isRemember) {
            number.setText(pref.getString("number", ""));
            passWord.setText(pref.getString("passWord", ""));
            rememberPass.setChecked(true);
        }

    }

    /**
     * 用于实现记住密码的方法，这里使用SharedPreference存放临时数据
     */
    private void isr() {
        editor = pref.edit();
        //检查复选框是否被选中
        if (rememberPass.isChecked()) {
            editor.putBoolean("remember_password", true);
            editor.putString("number", number.getText().toString());
            editor.putString("passWord", passWord.getText().toString());
        } else {
            editor.clear();
        }
        editor.apply();
    }


}
